package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.pojo.dto.FixDataJobDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.entity.AssembleProductEntity;
import com.xbongbong.saas.domain.entity.BomProductEntity;
import com.xbongbong.saas.domain.entity.ContractProductEntity;
import com.xbongbong.saas.domain.entity.InstockProductEntity;
import com.xbongbong.saas.domain.entity.InventoryProductEntity;
import com.xbongbong.saas.domain.entity.OpportunityProductEntity;
import com.xbongbong.saas.domain.entity.OutstockProductEntity;
import com.xbongbong.saas.domain.entity.ProductionOrderProductEntity;
import com.xbongbong.saas.domain.entity.PurchaseProductEntity;
import com.xbongbong.saas.domain.entity.ReturnedPurchaseProductEntity;
import com.xbongbong.saas.domain.entity.SupplierProductEntity;
import com.xbongbong.saas.domain.entity.TransferProductEntity;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.product.AssembleProductEnum;
import com.xbongbong.saas.enums.product.BomFinishProductEnum;
import com.xbongbong.saas.enums.product.ContractProductEnum;
import com.xbongbong.saas.enums.product.InstockProductEnum;
import com.xbongbong.saas.enums.product.InventoryProductEnum;
import com.xbongbong.saas.enums.product.OpportunityProductEnum;
import com.xbongbong.saas.enums.product.OutstockProductEnum;
import com.xbongbong.saas.enums.product.ProductionOrderFinishProductEnum;
import com.xbongbong.saas.enums.product.PurchaseProductEnum;
import com.xbongbong.saas.enums.product.ReturnPurchaseProductEnum;
import com.xbongbong.saas.enums.product.SupplierProductEnum;
import com.xbongbong.saas.enums.product.TransferProductEnum;
import com.xbongbong.saas.model.AssembleProductModel;
import com.xbongbong.saas.model.BomProductModel;
import com.xbongbong.saas.model.ContractProductModel;
import com.xbongbong.saas.model.InstockProductModel;
import com.xbongbong.saas.model.InventoryProductModel;
import com.xbongbong.saas.model.OpportunityProductModel;
import com.xbongbong.saas.model.OutstockProductModel;
import com.xbongbong.saas.model.ProductionOrderProductModel;
import com.xbongbong.saas.model.PurchaseProductModel;
import com.xbongbong.saas.model.ReturnedPurchaseProductModel;
import com.xbongbong.saas.model.SupplierProductModel;
import com.xbongbong.saas.model.TransferProductModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX+"/script/businessProduct")
public class BusinessProductScriptController {

    private static final Logger LOG = LoggerFactory.getLogger(BusinessProductScriptController.class);
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private AssembleProductModel assembleProductModel;
    @Resource
    private BomProductModel bomProductModel;
    @Resource
    private ContractProductModel contractProductModel;
    @Resource
    private InstockProductModel instockProductModel;
    @Resource
    private InventoryProductModel inventoryProductModel;
    @Resource
    private OpportunityProductModel opportunityProductModel;
    @Resource
    private OutstockProductModel outstockProductModel;
    @Resource
    private ProductionOrderProductModel productionOrderProductModel;
    @Resource
    private PurchaseProductModel purchaseProductModel;
    @Resource
    private ReturnedPurchaseProductModel returnedPurchaseProductModel;
    @Resource
    private SupplierProductModel supplierProductModel;
    @Resource
    private TransferProductModel transferProductModel;

    /**
     * Description: 修复业务产品表的data内数据
     * @param fixDataJobDTO
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2022/10/26 15:41
     * @since
     */
    @RequestMapping(value = "/fixBusinessProduct", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String fixBusinessProduct(@RequestBody @Valid FixDataJobDTO fixDataJobDTO) throws XbbException {
        XbbResponse<String> response;
        try {
            if (!proBaseConfig.getXbbCode().equals(fixDataJobDTO.getXbbCode())) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
            }
            List<String> corpidList = fixDataJobDTO.getCorpidList();
            Integer saasMark = fixDataJobDTO.getSaasMark();
            Integer businessType = fixDataJobDTO.getBusinessType();
            XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);

            LOG.warn("fixBusinessProduct business:" + xbbRefTypeEnum);
            for (String corpid : corpidList) {
                Map<String, Object> param = new HashMap<>();
                param.put(ParameterConstant.CORPID, corpid);
                param.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());

                long idGte = 0L;
                Integer start = 0;
                Integer pageNum = 1000;
                param.put("start", start);
                param.put("pageNum", pageNum);
                param.put("orderByStr", "id asc");
                if (Objects.equals(saasMark, SaasMarkEnum.SAAS.getCode())) {
                    switch (xbbRefTypeEnum) {
                        case ASSEMBLE_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<AssembleProductEntity> assembleProducts = assembleProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(assembleProducts)) {
                                    break;
                                }

                                idGte = assembleProducts.get(assembleProducts.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (AssembleProductEntity assembleProduct : assembleProducts) {

                                    JSONObject data = assembleProduct.getData();
                                    if (!Objects.isNull(assembleProduct.getProductId()) && !data.containsKey(AssembleProductEnum.PRODUCT.getAttr())) {
                                        data.put(AssembleProductEnum.PRODUCT.getAttr(), assembleProduct.getProductId());
                                    }
                                    if (!Objects.isNull(assembleProduct.getWarehouseId()) && !data.containsKey(AssembleProductEnum.WAREHOUSE.getAttr())) {
                                        data.put(AssembleProductEnum.WAREHOUSE.getAttr(), assembleProduct.getWarehouseId().toString());
                                    }
                                    if (!Objects.isNull(assembleProduct.getProductUnit()) && !data.containsKey(AssembleProductEnum.UNIT.getAttr())) {
                                        data.put(AssembleProductEnum.UNIT.getAttr(), assembleProduct.getProductUnit());
                                    }
                                    if (!Objects.isNull(assembleProduct.getProductNum()) && !data.containsKey(AssembleProductEnum.NUM.getAttr())) {
                                        data.put(AssembleProductEnum.NUM.getAttr(), assembleProduct.getProductNum());
                                    }
//                                data.put(AssembleProductEnum.STOCK.getAttr(), );
                                    if (!Objects.isNull(assembleProduct.getCost()) && !data.containsKey(AssembleProductEnum.COST.getAttr())) {
                                        data.put(AssembleProductEnum.COST.getAttr(), assembleProduct.getCost());
                                    }
//                                data.put(AssembleProductEnum.TOTAL_COST.getAttr(), );
                                    if (!Objects.isNull(assembleProduct.getMemo()) && !data.containsKey(AssembleProductEnum.MEMO.getAttr())) {
                                        data.put(AssembleProductEnum.MEMO.getAttr(), assembleProduct.getMemo());
                                    }
                                    if (!Objects.isNull(assembleProduct.getBatch()) && !data.containsKey(AssembleProductEnum.BATCH.getAttr())) {
                                        data.put(AssembleProductEnum.BATCH.getAttr(), assembleProduct.getBatch());
                                    }
                                    if (!Objects.isNull(assembleProduct.getProduceDate()) && !data.containsKey(AssembleProductEnum.PRODUCE_DATE.getAttr())) {
                                        data.put(AssembleProductEnum.PRODUCE_DATE.getAttr(), assembleProduct.getProduceDate());
                                    }
                                    if (!Objects.isNull(assembleProduct.getGuaranteePeriod()) && !data.containsKey(AssembleProductEnum.GUARANTEE_PERIOD.getAttr())) {
                                        data.put(AssembleProductEnum.GUARANTEE_PERIOD.getAttr(), assembleProduct.getGuaranteePeriod());
                                    }
//                                data.put(AssembleProductEnum.EXPIRE_DATE.getAttr(), );

                                    assembleProduct.setData(data);
                                }

                                assembleProductModel.updateBatch(assembleProducts, corpid);
                            }
                            break;
                        case BOM_BILL_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<BomProductEntity> bomProductEntities = bomProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(bomProductEntities)) {
                                    break;
                                }

                                idGte = bomProductEntities.get(bomProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (BomProductEntity bomProductEntity : bomProductEntities) {

                                    JSONObject dataObj = bomProductEntity.getData();
                                    if (!Objects.isNull(bomProductEntity.getProductId()) && !dataObj.containsKey(BomFinishProductEnum.PRODUCT.getAttr())) {
                                        dataObj.put(BomFinishProductEnum.PRODUCT.getAttr(), bomProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(bomProductEntity.getProductNum()) && !dataObj.containsKey(BomFinishProductEnum.NUM.getAttr())) {
                                        dataObj.put(BomFinishProductEnum.NUM.getAttr(), bomProductEntity.getProductNum());
                                    }
                                    if (!Objects.isNull(bomProductEntity.getMemo()) && !dataObj.containsKey(BomFinishProductEnum.MEMO.getAttr())) {
                                        dataObj.put(BomFinishProductEnum.MEMO.getAttr(), bomProductEntity.getMemo());
                                    }
                                    if (!Objects.isNull(bomProductEntity.getProductUnit()) && !dataObj.containsKey(BomFinishProductEnum.UNIT.getAttr())) {
                                        dataObj.put(BomFinishProductEnum.UNIT.getAttr(), bomProductEntity.getProductUnit());
                                    }

                                    bomProductEntity.setData(dataObj);
                                }

                                bomProductModel.updateBatch(bomProductEntities, corpid);
                            }
                            break;
                        case CONTRACT_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<ContractProductEntity> contractProductEntities = contractProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(contractProductEntities)) {
                                    break;
                                }

                                idGte = contractProductEntities.get(contractProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (ContractProductEntity contractProductEntity : contractProductEntities) {

                                    JSONObject data = contractProductEntity.getData();
                                    if (!Objects.isNull(contractProductEntity.getProductId()) && !data.containsKey(ContractProductEnum.PRODUCT.getAttr())) {
                                        data.put(ContractProductEnum.PRODUCT.getAttr(), contractProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(contractProductEntity.getProductUnit()) && !data.containsKey(ContractProductEnum.UNIT.getAttr())) {
                                        data.put(ContractProductEnum.UNIT.getAttr(), contractProductEntity.getProductUnit());
                                    }
                                    if (!Objects.isNull(contractProductEntity.getProductNum()) && !data.containsKey(ContractProductEnum.NUM.getAttr())) {
                                        data.put(ContractProductEnum.NUM.getAttr(), contractProductEntity.getProductNum());
                                    }
                                    if (!Objects.isNull(contractProductEntity.getPrice()) && !data.containsKey(ContractProductEnum.PRICE.getAttr())) {
                                        if (Objects.nonNull(contractProductEntity.getDiscount()) && !Objects.equals(contractProductEntity.getDiscount(), 0d)) {
                                            data.put(ContractProductEnum.PRICE.getAttr(), contractProductEntity.getPrice() / contractProductEntity.getDiscount() * 100);
                                        } else {
                                            data.put(ContractProductEnum.PRICE.getAttr(), contractProductEntity.getPrice());
                                        }
                                    }
                                    if (!Objects.isNull(contractProductEntity.getDiscount()) && !data.containsKey(ContractProductEnum.DISCOUNT.getAttr())) {
                                        data.put(ContractProductEnum.DISCOUNT.getAttr(), contractProductEntity.getDiscount());
                                    }
                                    data.put(ContractProductEnum.SELLING_PRICE.getAttr(), contractProductEntity.getPrice());
//                                data.put(ContractProductEnum.SUBTOTAL.getAttr(), );
                                    if (!Objects.isNull(contractProductEntity.getMemo()) && !data.containsKey(ContractProductEnum.MEMO.getAttr())) {
                                        data.put(ContractProductEnum.MEMO.getAttr(), contractProductEntity.getMemo());
                                    }
                                    if (!Objects.isNull(contractProductEntity.getOutstockNum()) && !data.containsKey(ContractProductEnum.OUTSTOCK_NUM.getAttr())) {
                                        data.put(ContractProductEnum.OUTSTOCK_NUM.getAttr(), contractProductEntity.getOutstockNum());
                                    }
                                    if (!Objects.isNull(contractProductEntity.getWaitOutStockNum()) && !data.containsKey(ContractProductEnum.WAIT_OUTSTOCK_NUM.getAttr())) {
                                        data.put(ContractProductEnum.WAIT_OUTSTOCK_NUM.getAttr(), contractProductEntity.getWaitOutStockNum());
                                    }
//                                data.put(ContractProductEnum.RETURN_NUM.getAttr(), );
//                                data.put(ContractProductEnum.STOCK.getAttr(), );
//                                data.put(ContractProductEnum.OUT_OF_STOCK.getAttr(), );
//                                data.put(ContractProductEnum.CONTRACT_STATUS.getAttr(),);
                                    if (!Objects.isNull(contractProductEntity.getShipmentsNum()) && !data.containsKey(ContractProductEnum.SHIPMENTS_NUM.getAttr())) {
                                        data.put(ContractProductEnum.SHIPMENTS_NUM.getAttr(), contractProductEntity.getShipmentsNum());
                                    }
//                                data.put(ContractProductEnum.PROMOTION.getAttr(), );
                                    if (!Objects.isNull(contractProductEntity.getStatus()) && !data.containsKey(ContractProductEnum.STATUS.getAttr())) {
                                        data.put(ContractProductEnum.STATUS.getAttr(), contractProductEntity.getStatus());
                                    }
                                    if (!Objects.isNull(contractProductEntity.getPurchaseNum()) && !data.containsKey(ContractProductEnum.PURCHASE_NUM.getAttr())) {
                                        data.put(ContractProductEnum.PURCHASE_NUM.getAttr(), contractProductEntity.getPurchaseNum());
                                    }

                                    contractProductEntity.setData(data);
                                }

                                contractProductModel.updateMulti(contractProductEntities, corpid);
                            }
                            break;
                        case INSTOCK_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<InstockProductEntity> instockProductEntities = instockProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(instockProductEntities)) {
                                    break;
                                }

                                idGte = instockProductEntities.get(instockProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (InstockProductEntity instockProductEntity : instockProductEntities) {

                                    JSONObject dataObj = instockProductEntity.getData();
                                    if (!Objects.isNull(instockProductEntity.getProductId()) && !dataObj.containsKey(InstockProductEnum.PRODUCT.getAttr())) {
                                        dataObj.put(InstockProductEnum.PRODUCT.getAttr(), instockProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(instockProductEntity.getWarehouseId()) && !dataObj.containsKey(InstockProductEnum.WAREHOUSE.getAttr())) {
                                        dataObj.put(InstockProductEnum.WAREHOUSE.getAttr(), instockProductEntity.getWarehouseId());
                                    }
                                    if (!Objects.isNull(instockProductEntity.getProductUnit()) && !dataObj.containsKey(InstockProductEnum.UNIT.getAttr())) {
                                        dataObj.put(InstockProductEnum.UNIT.getAttr(), instockProductEntity.getProductUnit());
                                    }
                                    if (!Objects.isNull(instockProductEntity.getProductNum()) && !dataObj.containsKey(InstockProductEnum.NUM.getAttr())) {
                                        dataObj.put(InstockProductEnum.NUM.getAttr(), instockProductEntity.getProductNum());
                                    }
                                    if (!Objects.isNull(instockProductEntity.getCost()) && !dataObj.containsKey(InstockProductEnum.COST.getAttr())) {
                                        dataObj.put(InstockProductEnum.COST.getAttr(), instockProductEntity.getCost());
                                    }
//                                dataObj.put(InstockProductEnum.TOTAL_COST.getAttr(), );
//                                dataObj.put(InstockProductEnum.STOCK.getAttr(), );
                                    if (!Objects.isNull(instockProductEntity.getMemo()) && !dataObj.containsKey(InstockProductEnum.MEMO.getAttr())) {
                                        dataObj.put(InstockProductEnum.MEMO.getAttr(), instockProductEntity.getMemo());
                                    }
                                    if (!Objects.isNull(instockProductEntity.getBatch()) && !dataObj.containsKey(InstockProductEnum.BATCH.getAttr())) {
                                        dataObj.put(InstockProductEnum.BATCH.getAttr(), instockProductEntity.getBatch());
                                    }
                                    if (!Objects.isNull(instockProductEntity.getProduceDate()) && !dataObj.containsKey(InstockProductEnum.PRODUCE_DATE.getAttr())) {
                                        dataObj.put(InstockProductEnum.PRODUCE_DATE.getAttr(), instockProductEntity.getProduceDate());
                                    }
                                    if (!Objects.isNull(instockProductEntity.getGuaranteePeriod()) && !dataObj.containsKey(InstockProductEnum.GUARANTEE_PERIOD.getAttr())) {
                                        dataObj.put(InstockProductEnum.GUARANTEE_PERIOD.getAttr(), instockProductEntity.getGuaranteePeriod());
                                    }
//                                dataObj.put(InstockProductEnum.EXPIRE_DATE.getAttr(), );

                                    instockProductEntity.setData(dataObj);
                                }

                                instockProductModel.updateMulti(instockProductEntities, corpid);
                            }
                            break;
                        case INVENTORY_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<InventoryProductEntity> inventoryProductEntities = inventoryProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(inventoryProductEntities)) {
                                    break;
                                }

                                idGte = inventoryProductEntities.get(inventoryProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (InventoryProductEntity inventoryProductEntity : inventoryProductEntities) {

                                    JSONObject dataObj = inventoryProductEntity.getData();
                                    if (!Objects.isNull(inventoryProductEntity.getProductId()) && !dataObj.containsKey(InventoryProductEnum.PRODUCT.getAttr())) {
                                        dataObj.put(InventoryProductEnum.PRODUCT.getAttr(), inventoryProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(inventoryProductEntity.getProductUnit()) && !dataObj.containsKey(InventoryProductEnum.UNIT.getAttr())) {
                                        dataObj.put(InventoryProductEnum.UNIT.getAttr(), inventoryProductEntity.getProductUnit());
                                    }
                                    if (!Objects.isNull(inventoryProductEntity.getStock()) && !dataObj.containsKey(InventoryProductEnum.STOCK.getAttr())) {
                                        dataObj.put(InventoryProductEnum.STOCK.getAttr(), inventoryProductEntity.getStock());
                                    }
                                    if (!Objects.isNull(inventoryProductEntity.getProductNum()) && !dataObj.containsKey(InventoryProductEnum.NUM.getAttr())) {
                                        dataObj.put(InventoryProductEnum.NUM.getAttr(), inventoryProductEntity.getProductNum());
                                    }
                                    if (!Objects.isNull(inventoryProductEntity.getInventoryId()) && !dataObj.containsKey(InventoryProductEnum.INVENTORY.getAttr())) {
                                        dataObj.put(InventoryProductEnum.INVENTORY.getAttr(), inventoryProductEntity.getInventoryId());
                                    }
                                    if (!Objects.isNull(inventoryProductEntity.getMemo()) && !dataObj.containsKey(InventoryProductEnum.MEMO.getAttr())) {
                                        dataObj.put(InventoryProductEnum.MEMO.getAttr(), inventoryProductEntity.getMemo());
                                    }
                                    if (!Objects.isNull(inventoryProductEntity.getBatch()) && !dataObj.containsKey(InventoryProductEnum.BATCH.getAttr())) {
                                        dataObj.put(InventoryProductEnum.BATCH.getAttr(), inventoryProductEntity.getBatch());
                                    }
                                    if (!Objects.isNull(inventoryProductEntity.getProduceDate()) && !dataObj.containsKey(InventoryProductEnum.PRODUCE_DATE.getAttr())) {
                                        dataObj.put(InventoryProductEnum.PRODUCE_DATE.getAttr(), inventoryProductEntity.getProduceDate());
                                    }
                                    if (!Objects.isNull(inventoryProductEntity.getGuaranteePeriod()) && !dataObj.containsKey(InventoryProductEnum.GUARANTEE_PERIOD.getAttr())) {
                                        dataObj.put(InventoryProductEnum.GUARANTEE_PERIOD.getAttr(), inventoryProductEntity.getGuaranteePeriod());
                                    }
//                                dataObj.put(InventoryProductEnum.EXPIRE_DATE.getAttr(), );

                                    inventoryProductEntity.setData(dataObj);
                                }

                                inventoryProductModel.updateMulti(inventoryProductEntities, corpid);
                            }
                            break;
                        case SALES_OPPORTUNITY_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<OpportunityProductEntity> opportunityProductEntities = opportunityProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(opportunityProductEntities)) {
                                    break;
                                }

                                idGte = opportunityProductEntities.get(opportunityProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (OpportunityProductEntity opportunityProductEntity : opportunityProductEntities) {

                                    JSONObject data = opportunityProductEntity.getData();
                                    if (!Objects.isNull(opportunityProductEntity.getProductId()) && !data.containsKey(OpportunityProductEnum.PRODUCT.getAttr())) {
                                        data.put(OpportunityProductEnum.PRODUCT.getAttr(), opportunityProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(opportunityProductEntity.getProductUnit()) && !data.containsKey(OpportunityProductEnum.UNIT.getAttr())) {
                                        data.put(OpportunityProductEnum.UNIT.getAttr(), opportunityProductEntity.getProductUnit());
                                    }
                                    if (!Objects.isNull(opportunityProductEntity.getProductNum()) && !data.containsKey(OpportunityProductEnum.NUM.getAttr())) {
                                        data.put(OpportunityProductEnum.NUM.getAttr(), opportunityProductEntity.getProductNum());
                                    }
                                    if (!Objects.isNull(opportunityProductEntity.getPrice()) && !data.containsKey(OpportunityProductEnum.PRICE.getAttr())) {
                                        data.put(OpportunityProductEnum.PRICE.getAttr(), opportunityProductEntity.getPrice());
                                    }
                                    if (!Objects.isNull(opportunityProductEntity.getDiscount()) && !data.containsKey(OpportunityProductEnum.DISCOUNT.getAttr())) {
                                        data.put(OpportunityProductEnum.DISCOUNT.getAttr(), opportunityProductEntity.getDiscount());
                                    }
//                                data.put(OpportunityProductEnum.SELLING_PRICE.getAttr(), );
//                                data.put(OpportunityProductEnum.SELL_TOTAL.getAttr(), );
                                    if (!Objects.isNull(opportunityProductEntity.getMemo()) && !data.containsKey(OpportunityProductEnum.MEMO.getAttr())) {
                                        data.put(OpportunityProductEnum.MEMO.getAttr(), opportunityProductEntity.getMemo());
                                    }

                                    opportunityProductEntity.setData(data);
                                }

                                opportunityProductModel.updateMulti(opportunityProductEntities, corpid);
                            }
                            break;
                        case OUTSTOCK_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<OutstockProductEntity> outstockProductEntities = outstockProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(outstockProductEntities)) {
                                    break;
                                }

                                idGte = outstockProductEntities.get(outstockProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (OutstockProductEntity outstockProductEntity : outstockProductEntities) {

                                    JSONObject dataObj = outstockProductEntity.getData();

                                    if (!Objects.isNull(outstockProductEntity.getProductId()) && !dataObj.containsKey(OutstockProductEnum.PRODUCT.getAttr())) {
                                        dataObj.put(OutstockProductEnum.PRODUCT.getAttr(), outstockProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(outstockProductEntity.getWarehouseId()) && !dataObj.containsKey(OutstockProductEnum.WAREHOUSE.getAttr())) {
                                        dataObj.put(OutstockProductEnum.WAREHOUSE.getAttr(), outstockProductEntity.getWarehouseId());
                                    }
                                    if (!Objects.isNull(outstockProductEntity.getProductUnit()) && !dataObj.containsKey(OutstockProductEnum.UNIT.getAttr())) {
                                        dataObj.put(OutstockProductEnum.UNIT.getAttr(), outstockProductEntity.getProductUnit());
                                    }
                                    if (!Objects.isNull(outstockProductEntity.getProductNum()) && !dataObj.containsKey(OutstockProductEnum.NUM.getAttr())) {
                                        dataObj.put(OutstockProductEnum.NUM.getAttr(), outstockProductEntity.getProductNum());
                                    }
//                                dataObj.put(OutstockProductEnum.STOCK.getAttr(), );
                                    if (!Objects.isNull(outstockProductEntity.getCost()) && !dataObj.containsKey(OutstockProductEnum.COST.getAttr())) {
                                        dataObj.put(OutstockProductEnum.COST.getAttr(), outstockProductEntity.getCost());
                                    }
//                                dataObj.put(OutstockProductEnum.TOTAL_COST.getAttr(), );
                                    if (!Objects.isNull(outstockProductEntity.getMemo()) && !dataObj.containsKey(OutstockProductEnum.MEMO.getAttr())) {
                                        dataObj.put(OutstockProductEnum.MEMO.getAttr(), outstockProductEntity.getMemo());
                                    }
                                    if (!Objects.isNull(outstockProductEntity.getBatch()) && !dataObj.containsKey(OutstockProductEnum.BATCH.getAttr())) {
                                        dataObj.put(OutstockProductEnum.BATCH.getAttr(), outstockProductEntity.getBatch());
                                    }
                                    if (!Objects.isNull(outstockProductEntity.getProduceDate()) && !dataObj.containsKey(OutstockProductEnum.PRODUCE_DATE.getAttr())) {
                                        dataObj.put(OutstockProductEnum.PRODUCE_DATE.getAttr(), outstockProductEntity.getProduceDate());
                                    }
                                    if (!Objects.isNull(outstockProductEntity.getGuaranteePeriod()) && !dataObj.containsKey(OutstockProductEnum.GUARANTEE_PERIOD.getAttr())) {
                                        dataObj.put(OutstockProductEnum.GUARANTEE_PERIOD.getAttr(), outstockProductEntity.getGuaranteePeriod());
                                    }
//                                dataObj.put(OutstockProductEnum.EXPIRE_DATE.getAttr(), );

                                    outstockProductEntity.setData(dataObj);
                                }

                                outstockProductModel.updateMulti(outstockProductEntities, corpid);
                            }
                            break;
                        case PRODUCTION_ORDER_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<ProductionOrderProductEntity> productionOrderProductEntities = productionOrderProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(productionOrderProductEntities)) {
                                    break;
                                }

                                idGte = productionOrderProductEntities.get(productionOrderProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (ProductionOrderProductEntity productionOrderProductEntity : productionOrderProductEntities) {

                                    JSONObject dataObj = productionOrderProductEntity.getData();
                                    if (!Objects.isNull(productionOrderProductEntity.getProductId()) && !dataObj.containsKey(ProductionOrderFinishProductEnum.PRODUCT.getAttr())) {
                                        dataObj.put(ProductionOrderFinishProductEnum.PRODUCT.getAttr(), productionOrderProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(productionOrderProductEntity.getProductUnit()) && !dataObj.containsKey(ProductionOrderFinishProductEnum.UNIT.getAttr())) {
                                        dataObj.put(ProductionOrderFinishProductEnum.UNIT.getAttr(), productionOrderProductEntity.getProductUnit());
                                    }
                                    if (!Objects.isNull(productionOrderProductEntity.getProductNum()) && !dataObj.containsKey(ProductionOrderFinishProductEnum.NUM.getAttr())) {
                                        dataObj.put(ProductionOrderFinishProductEnum.NUM.getAttr(), productionOrderProductEntity.getProductNum());
                                    }
//                dataObj.put(ProductionOrderFinishProductEnum.STOCK.getAttr(), );
                                    if (!Objects.isNull(productionOrderProductEntity.getMemo()) && !dataObj.containsKey(ProductionOrderFinishProductEnum.MEMO.getAttr())) {
                                        dataObj.put(ProductionOrderFinishProductEnum.MEMO.getAttr(), productionOrderProductEntity.getMemo());
                                    }

                                    productionOrderProductEntity.setData(dataObj);
                                }

                                productionOrderProductModel.updateMulti(productionOrderProductEntities, corpid);
                            }
                            break;
                        case PURCHASE_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<PurchaseProductEntity> purchaseProductEntities = purchaseProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(purchaseProductEntities)) {
                                    break;
                                }

                                idGte = purchaseProductEntities.get(purchaseProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (PurchaseProductEntity purchaseProductEntity : purchaseProductEntities) {

                                    JSONObject data = purchaseProductEntity.getData();
                                    if (!Objects.isNull(purchaseProductEntity.getProductId()) && !data.containsKey(PurchaseProductEnum.PRODUCT.getAttr())) {
                                        data.put(PurchaseProductEnum.PRODUCT.getAttr(), purchaseProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(purchaseProductEntity.getProductUnit()) && !data.containsKey(PurchaseProductEnum.UNIT.getAttr())) {
                                        data.put(PurchaseProductEnum.UNIT.getAttr(), purchaseProductEntity.getProductUnit());
                                    }
                                    if (!Objects.isNull(purchaseProductEntity.getProductNum()) && !data.containsKey(PurchaseProductEnum.NUM.getAttr())) {
                                        data.put(PurchaseProductEnum.NUM.getAttr(), purchaseProductEntity.getProductNum());
                                    }
                                    if (!Objects.isNull(purchaseProductEntity.getProductPrice()) && !data.containsKey(PurchaseProductEnum.PURCHASE_PRICE.getAttr())) {
                                        data.put(PurchaseProductEnum.PURCHASE_PRICE.getAttr(), purchaseProductEntity.getProductPrice());
                                    }
//                                data.put(PurchaseProductEnum.SUBTOTAL.getAttr(), );
                                    if (!Objects.isNull(purchaseProductEntity.getMemo()) && !data.containsKey(PurchaseProductEnum.MEMO.getAttr())) {
                                        data.put(PurchaseProductEnum.MEMO.getAttr(), purchaseProductEntity.getMemo());
                                    }
                                    if (!Objects.isNull(purchaseProductEntity.getInstockNum()) && !data.containsKey(PurchaseProductEnum.INSTOCK_NUM.getAttr())) {
                                        data.put(PurchaseProductEnum.INSTOCK_NUM.getAttr(), purchaseProductEntity.getInstockNum());
                                    }
                                    if (!Objects.isNull(purchaseProductEntity.getWaitInstockNum()) && !data.containsKey(PurchaseProductEnum.WAIT_INSTOCK_NUM.getAttr())) {
                                        data.put(PurchaseProductEnum.WAIT_INSTOCK_NUM.getAttr(), purchaseProductEntity.getWaitInstockNum());
                                    }
//                                data.put(PurchaseProductEnum.RETURN_NUM.getAttr(), );
//                                data.put(PurchaseProductEnum.RECEIVE_STATUS.getAttr(), );
                                    if (!Objects.isNull(purchaseProductEntity.getContractId()) && !data.containsKey(PurchaseProductEnum.CONTRACT_NO.getAttr())) {
                                        data.put(PurchaseProductEnum.CONTRACT_NO.getAttr(), purchaseProductEntity.getContractId());
                                    }

                                    purchaseProductEntity.setData(data);
                                }

                                purchaseProductModel.updateMulti(purchaseProductEntities, corpid);
                            }
                            break;
                        case RETURNEDPURCHASE_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<ReturnedPurchaseProductEntity> returnedPurchaseProductEntities = returnedPurchaseProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(returnedPurchaseProductEntities)) {
                                    break;
                                }

                                idGte = returnedPurchaseProductEntities.get(returnedPurchaseProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (ReturnedPurchaseProductEntity returnedPurchaseProductEntity : returnedPurchaseProductEntities) {

                                    JSONObject dataObj = returnedPurchaseProductEntity.getData();
                                    if (!Objects.isNull(returnedPurchaseProductEntity.getProductId()) && !dataObj.containsKey(ReturnPurchaseProductEnum.PRODUCT.getAttr())) {
                                        dataObj.put(ReturnPurchaseProductEnum.PRODUCT.getAttr(), returnedPurchaseProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(returnedPurchaseProductEntity.getProductUnit()) && !dataObj.containsKey(ReturnPurchaseProductEnum.UNIT.getAttr())) {
                                        dataObj.put(ReturnPurchaseProductEnum.UNIT.getAttr(), returnedPurchaseProductEntity.getProductUnit());
                                    }
                                    if (!Objects.isNull(returnedPurchaseProductEntity.getProductNum()) && !dataObj.containsKey(ReturnPurchaseProductEnum.NUM.getAttr())) {
                                        dataObj.put(ReturnPurchaseProductEnum.NUM.getAttr(), returnedPurchaseProductEntity.getProductNum());
                                    }
                                    if (!Objects.isNull(returnedPurchaseProductEntity.getProductPrice()) && !dataObj.containsKey(ReturnPurchaseProductEnum.PURCHASE_PRICE.getAttr())) {
                                        dataObj.put(ReturnPurchaseProductEnum.PURCHASE_PRICE.getAttr(), returnedPurchaseProductEntity.getProductPrice());
                                    }
//                                dataObj.put(ReturnPurchaseProductEnum.SUBTOTAL.getAttr(), );
                                    if (!Objects.isNull(returnedPurchaseProductEntity.getMemo()) && !dataObj.containsKey(ReturnPurchaseProductEnum.MEMO.getAttr())) {
                                        dataObj.put(ReturnPurchaseProductEnum.MEMO.getAttr(), returnedPurchaseProductEntity.getMemo());
                                    }
//                                dataObj.put(ReturnPurchaseProductEnum.SOURCE_DOC.getAttr(), );
                                    if (!Objects.isNull(returnedPurchaseProductEntity.getOutstockNum()) && !dataObj.containsKey(ReturnPurchaseProductEnum.OUTSTOCK_NUM.getAttr())) {
                                        dataObj.put(ReturnPurchaseProductEnum.OUTSTOCK_NUM.getAttr(), returnedPurchaseProductEntity.getOutstockNum());
                                    }
//                                dataObj.put(ReturnPurchaseProductEnum.WAIT_OUTSTOCK_NUM.getAttr(), );
//                                dataObj.put(ReturnPurchaseProductEnum.BATCH.getAttr(), );
//                                dataObj.put(ReturnPurchaseProductEnum.PRODUCE_DATE.getAttr(), );
//                                dataObj.put(ReturnPurchaseProductEnum.GUARANTEE_PERIOD.getAttr(), );
//                                dataObj.put(ReturnPurchaseProductEnum.EXPIRE_DATE.getAttr(), );
//                                dataObj.put(ReturnPurchaseProductEnum.WAREHOUSE.getAttr(), );
//                                dataObj.put(ReturnPurchaseProductEnum.RETURN_STATUS.getAttr(), );

                                    returnedPurchaseProductEntity.setData(dataObj);
                                }

                                returnedPurchaseProductModel.updateMulti(returnedPurchaseProductEntities, corpid);
                            }
                            break;
                        case SUPPLIER_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<SupplierProductEntity> supplierProductEntities = supplierProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(supplierProductEntities)) {
                                    break;
                                }

                                idGte = supplierProductEntities.get(supplierProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (SupplierProductEntity supplierProductEntity : supplierProductEntities) {

                                    JSONObject dataObj = supplierProductEntity.getData();
                                    if (!Objects.isNull(supplierProductEntity.getProductId()) && !dataObj.containsKey(SupplierProductEnum.PRODUCT.getAttr())) {
                                        dataObj.put(SupplierProductEnum.PRODUCT.getAttr(), supplierProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(supplierProductEntity.getUnit()) && !dataObj.containsKey(SupplierProductEnum.UNIT.getAttr())) {
                                        dataObj.put(SupplierProductEnum.UNIT.getAttr(), supplierProductEntity.getUnit());
                                    }

                                    supplierProductEntity.setData(dataObj);
                                }

                                supplierProductModel.updateBatch(supplierProductEntities, corpid);
                            }
                            break;
                        case TRANSFER_PRODUCT:
                            while (true) {
                                param.put("idGte", idGte);
                                List<TransferProductEntity> transferProductEntities = transferProductModel.findEntitys(param);
                                if (CollectionsUtil.isEmpty(transferProductEntities)) {
                                    break;
                                }

                                idGte = transferProductEntities.get(transferProductEntities.size() - 1).getId();
                                LOG.warn("fixBusinessProduct id:" + idGte);

                                for (TransferProductEntity transferProductEntity : transferProductEntities) {

                                    JSONObject dataObj = transferProductEntity.getData();
                                    if (!Objects.isNull(transferProductEntity.getProductId()) && !dataObj.containsKey(TransferProductEnum.PRODUCT.getAttr())) {
                                        dataObj.put(TransferProductEnum.PRODUCT.getAttr(), transferProductEntity.getProductId());
                                    }
                                    if (!Objects.isNull(transferProductEntity.getProductUnit()) && !dataObj.containsKey(TransferProductEnum.UNIT.getAttr())) {
                                        dataObj.put(TransferProductEnum.UNIT.getAttr(), transferProductEntity.getProductUnit());
                                    }
                                    if (!Objects.isNull(transferProductEntity.getProductNum()) && !dataObj.containsKey(TransferProductEnum.NUM.getAttr())) {
                                        dataObj.put(TransferProductEnum.NUM.getAttr(), transferProductEntity.getProductNum());
                                    }
                                    if (!Objects.isNull(transferProductEntity.getOutstockNum()) && !dataObj.containsKey(TransferProductEnum.OUTSTOCK_NUM.getAttr())) {
                                        dataObj.put(TransferProductEnum.OUTSTOCK_NUM.getAttr(), transferProductEntity.getOutstockNum());
                                    }
                                    if (!Objects.isNull(transferProductEntity.getInstockNum()) && !dataObj.containsKey(TransferProductEnum.INSTOCK_NUM.getAttr())) {
                                        dataObj.put(TransferProductEnum.INSTOCK_NUM.getAttr(), transferProductEntity.getInstockNum());
                                    }
//                                dataObj.put(TransferProductEnum.WAIT_INSTOCK_NUM.getAttr(), );
//                                dataObj.put(TransferProductEnum.STOCK.getAttr(), );
                                    if (!Objects.isNull(transferProductEntity.getMemo()) && !dataObj.containsKey(TransferProductEnum.MEMO.getAttr())) {
                                        dataObj.put(TransferProductEnum.MEMO.getAttr(), transferProductEntity.getMemo());
                                    }
                                    if (!Objects.isNull(transferProductEntity.getBatch()) && !dataObj.containsKey(TransferProductEnum.BATCH.getAttr())) {
                                        dataObj.put(TransferProductEnum.BATCH.getAttr(), transferProductEntity.getBatch());
                                    }
                                    if (!Objects.isNull(transferProductEntity.getProduceDate()) && !dataObj.containsKey(TransferProductEnum.PRODUCE_DATE.getAttr())) {
                                        dataObj.put(TransferProductEnum.PRODUCE_DATE.getAttr(), transferProductEntity.getProduceDate());
                                    }
                                    if (!Objects.isNull(transferProductEntity.getGuaranteePeriod()) && !dataObj.containsKey(TransferProductEnum.GUARANTEE_PERIOD.getAttr())) {
                                        dataObj.put(TransferProductEnum.GUARANTEE_PERIOD.getAttr(), transferProductEntity.getGuaranteePeriod());
                                    }
//                                dataObj.put(TransferProductEnum.EXPIRE_DATE.getAttr(), );
//                                dataObj.put(TransferProductEnum.TRANSFER_STATUS.getAttr(), );

                                    transferProductEntity.setData(dataObj);
                                }

                                transferProductModel.updateMulti(transferProductEntities, corpid);
                            }
                            break;
                        default:
                    }
                }
                LOG.warn("fixBusinessProduct finish!!!!");
            }
            return "success";
        } catch (XbbException e) {
            response = new XbbResponse<>(e);
        }
        return JSON.toJSONString(response);
    }

}
